package com.JUC;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class JUC09_BlockingQueue {
    public static void main(String[] args) throws InterruptedException {

//        List list = new ArrayList();

        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        //第一组
//        System.out.println(blockingQueue.add("a"));
//        System.out.println(blockingQueue.add("b"));
//        System.out.println(blockingQueue.add("c"));

        //当队列满再 add 会报 IllegalStateException:Queue full
//        System.out.println(blockingQueue.add("x"));
        //检查队首元素
//        System.out.println(blockingQueue.element());

//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());
        //当阻塞队列空时，再remove会抛NoSuchElementException
//        System.out.println(blockingQueue.remove());


//    第二组
//        System.out.println(blockingQueue.offer("a"));
//        System.out.println(blockingQueue.offer("b"));
//        System.out.println(blockingQueue.offer("c"));

        //当队列满再 offer 会返回 false
//        System.out.println(blockingQueue.offer("x"));
//        System.out.println(blockingQueue.poll());
//        System.out.println(blockingQueue.poll());
//        System.out.println(blockingQueue.poll());

        //当队列满再 poll 会返回 null
//        System.out.println(blockingQueue.poll());

//    第三组
//         blockingQueue.put("a");
//         blockingQueue.put("b");
//         blockingQueue.put("c");

        //当队列满再 put 会一直阻塞，不会返回，不会抛异常
        //直到有空位
//         blockingQueue.put("x");

//        System.out.println(blockingQueue.take());
//        System.out.println(blockingQueue.take());
//        System.out.println(blockingQueue.take());

        //当队列满再 take 会一直阻塞，不会返回，不会抛异常
        //直到有值插入
//        System.out.println(blockingQueue.take());

//    第四组
//        System.out.println(blockingQueue.offer("a"));
//        System.out.println(blockingQueue.offer("b"));
//        System.out.println(blockingQueue.offer("c"));

        //当队列满再 take 会阻塞一段时间，一段时间会返回 true / false
//        System.out.println(blockingQueue.offer("a",3, TimeUnit.SECONDS));

    }
}
